package com.dm.logistics.loginmanage.controller;

import com.dm.logistics.loginmanage.service.LoginService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional
@Controller
@CrossOrigin
public class LoginController {

    @Autowired
    private LoginService loginService;

    @RequestMapping(value="/toLogin",method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView toLogin(){
        ModelAndView mav = new ModelAndView();
        mav.setViewName("login");
        return mav;
    }

    @RequestMapping(value="/login",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> goLogin(@RequestBody Map<String,Object> param, HttpServletRequest request){
        Map<String,Object> statusMap = loginService.queryUserInfosByUsername(param,request);
        return statusMap;
    }

    @RequestMapping(value="/initPage",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String initPage( @RequestBody Map<String,Object> param){
        String status = param.get("status")!=null? param.get("status").toString():null;
        JSONObject js= new JSONObject();
        if(status!=null){
            List<Map<String,Object>> resultList = loginService.queryZDStore(param);

            List<Map<String,Object>> tempResultList = loginService.queryZDStore(param);
//            tempResultList.get(0).put("selected",true);

            Map<String,Object> tempMap = new HashMap<>();
            tempMap.put("ming","全部");
            tempMap.put("selected",true);
            resultList.add(tempMap);

            //查询类别下拉框信息
            Map<String,Object> EquTypeMap = new HashMap<String, Object>();
            EquTypeMap.put("EquType","类别");
            List<Map<String,Object>> EquTypeResultList = loginService.queryEquType(EquTypeMap);
//            EquTypeResultList.get(0).put("selected",true);

            Map<String,Object> resultMap = new HashMap<String, Object>();
            resultMap.put("arrdata",resultList);
            resultMap.put("arrdata2",tempResultList);
            resultMap.put("equtypedata",EquTypeResultList);
            js = JSONObject.fromObject(resultMap);
        }else{
            Map<String,Object> resultMap = new HashMap<String, Object>();
            resultMap.put("status","-1");
            js = JSONObject.fromObject(resultMap);
        }

        return js.toString();
    }

    /**
         * @Description 设置下拉框参数
         * @Author zhangxue
         * @Date 18:05 2018/5/24
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/saveSettings",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String saveSettings(@RequestBody Map<String,Object> map) {
        Map<String,Object> resultMap = loginService.saveSettings(map);
        JSONObject jsonObject = JSONObject.fromObject(resultMap);
        return jsonObject.toString();
    }

}
